package com.jysz.certificate.persistence.dao.cm;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.jysz.certificate.persistence.dao.AbstractBaseDao;
import com.jysz.certificate.persistence.enums.CertificateStatus;
import com.jysz.certificate.persistence.model.cm.Certificate;

@Repository
public class CertificateDao extends AbstractBaseDao<Certificate, Long> {
	
	public Certificate getCertificateById(Long id){
		Criteria c = this.getSession().createCriteria(Certificate.class);
		c.add(Restrictions.eq("id", id));
		Certificate n = (Certificate) c.uniqueResult();
		return n;
	}

	public Certificate getUpdateCertificate(Long id,CertificateStatus status) {
		Criteria c = this.getSession().createCriteria(Certificate.class);
		c.add(Restrictions.eq("certification.id", id));
		c.add(Restrictions.eq("status", status));
		
		Certificate n = (Certificate) c.uniqueResult();
		return n;
	}
	
	public boolean getCertifite(Certificate cer){
		Criteria c = this.createCriteria();
		if(cer.getId()!=null){
			c.add(Restrictions.ne("id", cer.getId()));
		}
		c.add(Restrictions.eq("status", CertificateStatus.NOTSTART));
		c.add(Restrictions.disjunction().add(Restrictions.eq("certificationNum", 
				cer.getCertificationNum())).add(Restrictions.eq("chassis", cer.getChassis())));
		List list = c.list();
		if(list!=null&&list.size()>0){
			return false;
		}
		return true;
	}
	
}
